﻿Imports System.Data.SqlClient
Public Class frm_Login
    Dim CN As New SqlConnection


    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Dim cmd As New SqlCommand
        Dim Reader As SqlDataReader = Nothing
        Dim strPassword As String = ""
        Dim blnAdmin As Boolean = False
        cmd.Connection = CN

        'TA - Select table to load from acording to the radio button selected.
        If Student_RadioButton.Checked = True Then
            cmd.CommandText = "SELECT Password FROM STUDENTS WHERE ID = @ID"
        ElseIf Faculty_RadioButton.Checked = True Then
            cmd.CommandText = "SELECT Password FROM TEACHER WHERE ID = @ID"
            blnAdmin = isAdmin(tb_Username.Text)
        End If

        cmd.Parameters.AddWithValue("@ID", tb_Username.Text)


        'TA - Connect to the database, if unsuccessfull report error and close program
        Try
            CN.Open()
        Catch ex As Exception
            MessageBox.Show("Cannot Connect to Data Base: Error - " & ex.Message)
        End Try

        Try
            Reader = cmd.ExecuteReader
            While Reader.Read
                strPassword = Convert.ToString(Reader.Item("Password")).Trim
            End While
        Catch ex As Exception
        Finally
            'TA - Closes connection with the database if it is still open
            If CN.State = ConnectionState.Open Then CN.Close()
            'Reader.Close()
        End Try


        If strPassword = tb_Password.Text.Trim Then
            If Faculty_RadioButton.Checked = True Then
                If blnAdmin Then
                    Me.Hide()
                    frmMain.ShowDialog()
                Else
                    Me.Hide()
                    Dim frmTeacherSchedule As New frmTeacherSchedule(Convert.ToInt32(tb_Username.Text.Trim))
                    frmTeacherSchedule.ShowDialog()
                End If
            Else
                Me.Hide()
                Dim frmStudentSchedule As New frmStudentSchedule(Convert.ToInt32(tb_Username.Text.Trim))
                frmStudentSchedule.ShowDialog()
            End If
        Else
            MessageBox.Show("Invalid Login")
        End If
        'TA - Closes the form window.
        Me.Close()

    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub frm_Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CN.ConnectionString = My.Settings.IT308ConnectionString

        Try
            CN.Open()
        Catch ex As Exception
            My.Settings.SetUserOverride("IT308ConnectionString", InputBox("Default Database Address Failed.  Please enter new address.", "Connection String", My.Settings.IT308ConnectionString.ToString))
            My.Settings.Save()
        Finally
            If CN.State = ConnectionState.Open Then CN.Close()
        End Try

    End Sub
    Private Function isAdmin(ByVal id As String) As Boolean
        Dim cmd As New SqlCommand
        Dim Reader As SqlDataReader = Nothing
        Dim intAdmin As Integer
        cmd.Connection = CN
        cmd.CommandText = "SELECT Admin FROM TEACHER WHERE ID = @ID"
        cmd.Parameters.AddWithValue("@ID", id)

        Try
            CN.Open()
        Catch ex As Exception
            MessageBox.Show("Cannot Connect to Data Base: Error - " & ex.Message)
        End Try

        Try
            Reader = cmd.ExecuteReader
            While Reader.Read
                intAdmin = Convert.ToInt32(Reader.Item("Admin"))
            End While
        Catch ex As Exception
            MessageBox.Show("Cannot Connect to Data Base: Error - " & ex.Message)
        Finally
            If CN.State = ConnectionState.Open Then CN.Close()
        End Try

        If intAdmin = 1 Then
            Return True
        Else
            Return False
        End If
    End Function
End Class
